import { _decorator, animation, Animation, CCFloat, color, Color, Component, director, Node, PhysicsSystem2D, random, randomRange, randomRangeInt, Rect, Sprite, UITransform, Vec2, Vec3 } from 'cc';
import { StateDefine } from '../../Actor/Config';
import { Enemy } from '../../Actor/Enemy/Enemy';
const { ccclass, property } = _decorator;

@ccclass('Clone_Skill_Controller')
export class Clone_Skill_Controller extends Component {
    private sp: Sprite = null;
    private cloneTimer: number = 1;
    private colorLoosingSpeed: number = 10;

    @property(Animation) anim: Animation = null;

    @property(Node) attackCheck: Node = null;
    @property(CCFloat) attackR: number = 20;
    private closesEnemy:Enemy = null;
    private facingDir:number = 1;
    protected onLoad(): void {
        this.sp = this.getComponent(Sprite);
    }
    protected update(dt: number): void {
        this.cloneTimer -= dt;
        if (this.cloneTimer < 0) {
            this.sp.color = new Color(255,255,255,this.sp.color.a-this.colorLoosingSpeed);
        }
        if (this.sp.color.a <= 1) {
            this.node.destroy()
        }
    }
    setUpClone(pos: Vec3, _cloneDuration: number, _colorLoosingSpeed: number, _canAttack: boolean) {
        
        this.node.setWorldPosition(pos);
        this.cloneTimer = _cloneDuration;
        this.colorLoosingSpeed = _colorLoosingSpeed;
        this.FaceClosesTarget();
        if (_canAttack) {
            this.anim.play(StateDefine.Attack + randomRangeInt(1, 4));
        }
    }

    public AnimationTrigger(): void {
        this.cloneTimer = 0;
    }
    AttackTrigger() {
        const pos = this.attackCheck.getWorldPosition();
        //偏移问题
        const box = new Rect(pos.x+(this.facingDir-1)*this.attackR/2, pos.y, this.attackR, this.attackR);
        const colliderList = PhysicsSystem2D.instance.testAABB(box);
        for (const collider of colliderList) {
            const enemyComponent = collider.getComponent(Enemy);
            if (enemyComponent) {
                console.info(collider.name);
                enemyComponent.damage();
            }
        }
    }
    FaceClosesTarget(){
        let closes = Number.MAX_VALUE;
        const pos = this.node.getWorldPosition();
        const box = new Rect(pos.x-2*this.attackR, pos.y, this.attackR*4, this.attackR);
        const colliderList = PhysicsSystem2D.instance.testAABB(box);
        for (const collider of colliderList) {
            const enemyComponent = collider.getComponent(Enemy);
            if (enemyComponent) {
                const temp = Vec2.distance(pos,enemyComponent.node.getWorldPosition())
                if(temp<closes) {
                    
                    closes = temp;
                    this.closesEnemy = enemyComponent;
                }
            }
        }
        if(this.closesEnemy!= null){
            this.facingDir = 1;
            if(this.closesEnemy.node.getWorldPosition()<pos){
                this.facingDir = -1;
            }
            this.node.scale = new Vec3(this.facingDir,1,0);
        }
    }
}


